'use strict';

module.exports = app => {
  class OrderController extends app.Controller {
    async index (ctx) {
      let me = await ctx.helper.rest.fetchMe({ctx})
      if (!me) {
        return ctx.helper.abortOnError({ctx, error: ctx.errors.UNLOGINED})
      }

      let { user_id } = ctx.params

      if (me.id !== user_id) {
        return ctx.helper.abortOnError({ctx, error: ctx.errors.NOT_FOR_YOU, message: 'you cannot query those information.'})
      }

      let { offset = 0, size = 10 } = ctx.request.query || {}
      offset = parseInt(offset)
      size = parseInt(size)

      let [error] = ctx.app.validator.validate({
        offset: ctx.validation.offset,
        size: ctx.validation.size
      }, {offset, size}) || []

      if (ctx.helper.abortOnError({ctx, error})) {
        return
      }

      let list = await ctx.model.order.find({$or: [{user_id}, {uid: me.uid}]}).skip(offset).limit(size)
      let total = await ctx.model.order.find({$or: [{user_id}, {uid: me.uid}]}).count()

      ctx.helper.rest.resp({
        ctx,
        data: {
          total,
          list: list.map(x => x.toObject({
            virtuals: true,
            transform: (doc, ret) => {
              ret = ctx.helper.mongoose.tranformId(doc, ret)
              ret = ctx.helper.mongoose.tranformTime(doc, ret)
              return ret
            }
          }))
        }
      })
    }

    async show (ctx) {

    }

    async create (ctx) {
      let me = await ctx.helper.rest.fetchMe({ctx})
      if (!me) {
        return ctx.helper.abortOnError({ctx, error: ctx.errors.UNLOGINED})
      }

      if (ctx.is_self_admin) {
        return ctx.helper.abortOnError({ctx, error: ctx.errors.UNNECESSARY})
      }

      let { user_id } = ctx.params

      if (me.id !== user_id) {
        return ctx.helper.abortOnError({ctx, error: ctx.errors.NOT_FOR_YOU})
      }

      let [error] = ctx.app.validator.validate(ctx.resource_validation.order.create, ctx.request.body) || []

      if (ctx.helper.abortOnError({ctx, error})) {
        return
      }

      let {
        title,
        target_country,
        app_link,
        keywords,
        email_addr,
        need,
        is_help = false
      } = ctx.request.body

      let new_order = new ctx.model.order({uid: me.uid, user_id: me.id, title, target_country, app_link, keywords, email_addr, need, is_help})

      let result
      try {
        result = await new_order.save()
      } catch (error) {
        ctx.app.logger.info(error)
        result = null
      }

      // console.info('result', result)
      if (!result) {
        return ctx.helper.abortOnError({ctx, error: ctx.errors.UPDATE_FAIL})
      }

      ctx.helper.rest.resp({
        ctx,
        data: new_order.toObject({
          virtuals: true,
          transform: (doc, ret) => {
            ret = ctx.helper.mongoose.tranformId(doc, ret)
            ret = ctx.helper.mongoose.tranformTime(doc, ret)
            return ret
          }
        })
      })
    }
  }

  return OrderController;
};
